﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// RecordInList.xaml 的交互逻辑
    /// </summary>
    public partial class RecordInOutList : Page
    {
        private int pageNo = 1;
        private int pagePer = 5000;
        private int pages = 1;
        private DataTable _dt = null;

        public RecordInOutList()
        {
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString());
            InitializeComponent();
        }

        private void btn_query_Click(object sender, RoutedEventArgs e)
        {
            bool? exact = this.cbx_exact.IsChecked;
            query(this.txt_name.Text, this.txt_code.Text, 
                Utils.ParseDateTime(this.txt_time_begin.Text != "" ? 
                Convert.ToDateTime(this.txt_time_begin.Text).ToString("yyyy-MM-dd HH:mm") : this.txt_time_begin.Text),
                Utils.ParseDateTime(this.txt_time_end.Text != "" ? 
                Convert.ToDateTime(this.txt_time_end.Text).ToString("yyyy-MM-dd HH:mm") : this.txt_time_end.Text),
                exact);
        }

        private void query(string goods_name, string goods_code, DateTime? create_time_begin, DateTime? create_time_end, bool? exact)
        {
            Dictionary<string, object> param = new Dictionary<string, object>();
            string sql = @"select t.* from
            (
            select t2.record_number, t2.type, t2.create_time, '入库' as io_text, t1.quantity as change, t1.memo as change_memo,
            t3.code,
            t3.name,
            t3.type as goods_type,
            t3.size,
            t3.weight,
            t3.value,
            t3.client
            from t_record_in_item t1 inner JOIN t_record_in t2 on t1.record_in_id = t2.id inner join t_goods t3 on t1.goods_id = t3.id
            UNION all
            select t2.record_number, t2.type, t2.create_time, '出库' as io_text, t1.quantity as change, t1.memo as change_memo,
            t3.code,
            t3.name,
            t3.type as goods_type,
            t3.size,
            t3.weight,
            t3.value,
            t3.client
             from t_record_out_item t1 inner JOIN t_record_out t2 on t1.record_out_id = t2.id inner join t_goods t3 on t1.goods_id = t3.id
            )
            t where 1 = 1";
            if (!string.IsNullOrWhiteSpace(goods_name))
            {
                if (exact != null && exact == true)
                {
                    sql += " and name = @name";
                }
                else
                {
                    sql += " and name like '%' + @name + '%' ";
                }
                param.Add("@name", goods_name);
            }
            if (!string.IsNullOrWhiteSpace(goods_code))
            {
                if (exact != null && exact == true)
                {
                    sql += " and code = @code";
                }
                else
                {
                    sql += " and code like '%' + @code + '%' ";
                }
                param.Add("@code", goods_code);
            }
            if (create_time_begin != null)
            {
                sql += " and create_time >= @create_time_begin";
                param.Add("@create_time_begin", create_time_begin);
            }
            if (create_time_end != null)
            {
                sql += " and create_time <= @create_time_end";
                param.Add("@create_time_end", create_time_end);
            }

            this._dt = DBHelper.GetDataTable(sql, param, "create_time desc", pagePer, (pageNo - 1) * pagePer);
            dataGrid.ItemsSource = this._dt.DefaultView;
            int count = DBHelper.GetCount(sql, param);

            this.pages = (count + pagePer - 1) / pagePer;
            //this.lbl_page.Content = String.Format("共{0}条 每页{1}条，当前{2}/{3}", count, pagePer, pageNo, pages);
        }

        private void btn_refresh_Click()
        {
            query("","",null,null,false);
        }

        private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void dataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            dataGrid_SelectionChanged(null, null);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            query("", "", null, null, false);
        }

        private void btn_export_Click()
        {

            if (this._dt == null)
            {
                MessageBox.Show("没有数据可以导出");
                return;
            }

            Dictionary<string, string> mapping = new Dictionary<string, string>();
            mapping.Add("record_number", "单号");
            mapping.Add("type", "出入类型");
            mapping.Add("create_time", "业务日期");
            mapping.Add("io_text", "出/入");
            mapping.Add("change", "库存变动");
            mapping.Add("change_memo", "备注");
            mapping.Add("code", "物品编码");
            mapping.Add("name", "物品名称");
            mapping.Add("goods_type", "物品类型");
            mapping.Add("size", "物品尺寸");
            mapping.Add("weight", "物品重量");
            mapping.Add("value", "声明价值");
            mapping.Add("client", "客户");

            System.Windows.Forms.SaveFileDialog sd = new System.Windows.Forms.SaveFileDialog();
            sd.Filter = "Excel 2003 文档|*.xls";
            if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (File.Exists(sd.FileName))
                {
                    File.Delete(sd.FileName);
                }
                ExcelHelper.TableToExcel(this._dt, mapping, sd.FileName);
                if (MessageBox.Show("导出完毕，是否现在打开？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    System.Diagnostics.Process.Start(sd.FileName);
                }
            }
        }
    }
}
